package com.bayleaf.web.regexFinder;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

import com.bayleaf.core.BayleafServlet;
import com.bayleaf.core.Model;


@WebServlet(name="regexFinder",
        urlPatterns={"/regexFinder"},
        initParams={ @WebInitParam(name="title", value="Regular Expression Finder"), @WebInitParam(name="jsp", value="/regexFinder/RegexFinder") } )
public class RegexFinderServlet extends BayleafServlet {
	
    @Override
    public String process(Model model) throws Exception{
    	
    	String regex = model.getString("regex");
    	String input = model.getString("input");
    	System.out.println("input data : "+input);
    	System.out.println("regex data : "+regex);
    	String result = "";
    	int startOffset = 0;
    	int endOffset = 0;
    	boolean first = true;

    	try{
    		Matcher matcher = regexFind(regex, input);
    		while(matcher.find()){
    			startOffset = matcher.start();
				if(first){
					result += input.substring(0, startOffset);
					first = false;
				}else{
					result += input.substring(endOffset, startOffset);
				}
				endOffset = matcher.end();
				
				result += "<span style='background-color:#FFCCFF;'>"+input.substring(startOffset, endOffset)+"</span>";
    		}
    		result += input.substring(endOffset);
    		
    		if(matcher.groupCount() <1){
    			model.setString("output", input);
    			return null;
    			
    		}
    		
    	}catch(PatternSyntaxException e){
    		model.setString("output", "Invalid regex");
            return null;
    	}
    	
    	
    	
    	model.setString("output", result);
    	
        return null;
    }

	/**
	 * 정규식으로 스트링의 값을 찾음
	 * @param regex
	 * @param input
	 * @return matcher
	 */
	public Matcher regexFind(String regex, String input) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(new StringBuilder(input));
		return matcher;
	}
    
//    int getRegexOptions() {
//        int Options = 0;
//        if (checkCanonEquivalence.isSelected()) {
//          // In Unicode, certain characters can be encoded in more than one way.
//          // Many letters with diacritics can be encoded as a single character
//          // identifying the letter with the diacritic, and encoded as two
//          // characters: the letter by itself followed by the diacritic by itself
//          // Though the internal representation is different, when the string is
//          // rendered to the screen, the result is exactly the same.
//          Options |= Pattern.CANON_EQ;
//        }
//        if (checkCaseInsensitive.isSelected()) {
//          // Omitting UNICODE_CASE causes only US ASCII characters to be matched
//          // case insensitively.  This is appropriate if you know beforehand that 
//          // the subject string will only contain US ASCII characters
//          // as it speeds up the pattern matching.
//          Options |= Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
//        }
//        if (checkDotAll.isSelected()) {
//          // By default, the dot will not match line break characters.
//          // Specify this option to make the dot match all characters, 
//          // including line breaks
//          Options |= Pattern.DOTALL;
//        }
//        if (checkMultiLine.isSelected()) {
//          // By default, the caret ^, dollar $  only match at the start 
//          // and the end of the string.  Specify this option to make ^ also match 
//          // after line breaks in the string, and make $ match before line breaks.
//          Options |= Pattern.MULTILINE;
//        }
//        return Options;
//      }
    
}
